<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use Think\Controller;
use Think\Controller\RestController;
use Com\Wechat\Oauth;

/**
 * 前台公共控制器
 * 为防止多分组Controller名称冲突，公共Controller名称统一使用分组名称
 */
class HomeController extends RestController {

	/* 空操作，用于输出404页面 */
	public function _empty(){
		$this->redirect('Index/index');
	}
	public function dateGet($date_start,$date_end){
        $date = $date_start;
        while($date <= $date_end){
        	$result[] = $date;
            $date = date('Y-m-d',strtotime("$date +1 day"));    
        }
        return $result;
    }

    protected function _initialize(){
        /* 读取站点配置 */
        $config = api('Config/lists');
        C($config); //添加配置

        if(!C('WEB_SITE_CLOSE')){
            $this->error('站点已经关闭，请稍后访问~');
        }
    }

	/* 用户登录检测 */
	public function login(){

		return $this->checkLogin();
  	}

	private function get_url() {
	    $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
	    $php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
	    $path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
	    $relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : $path_info);
	    return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
 	}
 	public function checkLogin(){
 		$uid =is_login();
 		if($uid){
 			return $uid;
 		}
 		else{
 			$this->response(array('info'=>'未登录'),'json',402);
 		}
 	}
	public function auth_check(){
 		if (is_login()) {
			return is_login();
		}
		else{
			$this->oauth();
		}
 	}

	public function oauth(){
		$uid =is_login();
		$ucenter_member =M('ucenter_member')->where('id='.$uid)->find();
		if ($uid&&$ucenter_member) {
			return;
		}
		$oauth = new Oauth();
		$oauth->init($this->get_url());
		
		//通过code获得openid
		if (!isset($_GET['code'])){			
			session('weixin_login_url',$this->get_url());
			$url = $oauth->get_code_by_authorize(1);//获取code，会重定向到当前页。若需传参，使用$state变量传参。
			exit();
		} else {
			//获取code码，以获取openid
		  	$code = $_GET['code'];
			$user_info = $oauth->get_userinfo_by_authorize($code);
			$this->checkIsUserByOpenId($user_info);
		}

	}
	private function checkIsUserByOpenId($user_info){
		$ucenter_member = D('UcenterMember');
		$openid 	= $user_info['openid'];
		$nickname 	= $user_info['nickname'];
		$headimgurl	= $user_info['headimgurl'];
		$umember = $ucenter_member ->checkIsUserByOpenId($openid);
		if($umember){
			$uid = $umember['id'];
		}else{
			$uid = $ucenter_member ->register($nickname,$openid,$headimgurl);
		}
		$user['uid'] =$uid;
        session('user_auth_sign' ,data_auth_sign($user));
        session('user_auth',$user);
        $url = $this->get_url();
        redirect($url);

    }
    public function securityTest(){
    	$uid = is_login();
	    if(!$uid){
	      $this->response(array('info'=>'未登录，或者未使用微信浏览器开启页面'),json,401);
	    }
  	}

  	public function is_in_intersection($a,$b,$c,$d){
  		if($a<=$c && $b>=$c){
	        return true; 
	    }
	    if($a>$c && $b>$a && $a<=$d){
	        return true; 
	    }
	    return false;
  	}

  	public function is_set_info($json,$value,$field,$error,$default = 0){
  		if($json['success']){
  			if($value == null){
                if($default !='-1'){
                	if($default >=0){
                		$json['info'][$field] = $default;
                	}else{
                		$json['info'][$field] = null;
                	}
                }else{
                	$json['success'] = false;
                	$json['error'] = $error;
                }               
            }else{
                $json['info'][$field] = $value;
            }
        }
  		return $json; 	
  	}
}
